package com.wxit.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxit.commonutils.R;
import com.wxit.staservice.client.UcenterClient;
import com.wxit.staservice.entity.StatisticsDaily;
import com.wxit.staservice.mapper.StatisticsDailyMapper;
import com.wxit.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 吴杰
 * @since 2021-12-24
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //添加记录之前删除相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用，得到每一天的注册人数
        R registerCount = ucenterClient.countRegister(day);
        Integer count = (Integer) registerCount.getData().get("countRegister");

        //把获取到的数据添加到数据库中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(count); //注册人数
        sta.setDateCalculated(day); //统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //因为返回的由两部分数据，日期  日期对应的数量
        //前端要求数组json结构，对应后端Java代码是list集合
        //创建两个list集合，一个日期list,一个数量list

        ArrayList<String> data_calculatedList = new ArrayList<>();
        ArrayList<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据的list集合
        staList.stream().forEach(statisticsDaily -> {
            //封装日期list集合，进行封装
            data_calculatedList.add(statisticsDaily.getDateCalculated());
            //封装对应的数量
            switch (type){
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        });

        HashMap<String, Object> map = new HashMap<>();
        map.put("data_calculatedList",data_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
